/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package phongmodel;

/**
 *
 * @author Grzesiek
 */
public class Vector3D {
    
    private double x;
    private double y;
    private double z;
    
    public Vector3D(){
        this.x = 0;
        this.y = 0;
        this.z = 0;
    }
    
    public Vector3D(double x, double y, double z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    public Vector3D(Point3D p){
        this.x = p.getX();
        this.y = p.getY();
        this.z = p.getZ();
    }
    
    public Vector3D(Point3D start, Point3D end){
        this.x = start.getX() - end.getX();
        this.y = start.getY() - end.getY();
        this.z = start.getZ() - end.getZ();
    }
    
    public static Vector3D sub(Vector3D v1, Vector3D v2){
        return new Vector3D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
    }

    
    public static Vector3D add(Vector3D v1, Vector3D v2){
        return new Vector3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
    }

    
    public static Vector3D cross(Vector3D v1, Vector3D v2){ 
        double x,y,z;

        x = v1.y*v2.z - v1.z*v2.y;
        y = v2.x*v1.z - v2.z*v1.x;
        z = v1.x*v2.y - v1.y*v2.x;

        return new Vector3D(x,y,z);
    }
    
    public static double dot(Vector3D v1, Vector3D v2){
        return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
    }
    
    public void normalize(){
        double norm;

        norm = 1.0/length();
        this.x *= norm;
        this.y *= norm;
        this.z *= norm;
    }
    
    public double length(){
        return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
    }
    
    public void scalarMulti(double scalar){
        this.x *= scalar;
        this.y *= scalar;
        this.z *= scalar;
    }
    
    public void negate(){
        this.x = -x;
        this.y = -y;
        this.z = -z;
    }
    
    public double getX(){
        return this.x;
    }
    
    public double getY(){
        return this.y;
    }
    
    public double getZ(){
        return this.z;
    }
    
    public void setX(double x){
        this.x = x;
    }
    
    public void setY(double y){
        this.y = y;
    }
    
    public void setZ(double z){
        this.z = z;
    }
    public Vector3D copy(){
        Vector3D copy = new Vector3D();
            
        copy.setX(this.x);
        copy.setY(this.y);
        copy.setZ(this.z);
        
        return copy;
    }
}
